package com.atlin.project.service.impl;

import com.atlin.project.common.ErrorCode;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.service.TestUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atlin.project.model.domain.TestUser;
import com.atlin.project.mapper.TestUserMapper;
import org.springframework.stereotype.Service;

/**
 * @author atlin
 * @description 针对表【test_user(测试)】的数据库操作Service实现
 * @createDate 2025-03-18 22:19:01
 */
@Service
public class TestUserServiceImpl extends ServiceImpl<TestUserMapper, TestUser>
        implements TestUserService {

    @Override
    public boolean addUser(TestUser user) {
//        int insert = baseMapper.insert(user);
        LambdaQueryWrapper<TestUser> wrapper = new LambdaQueryWrapper<TestUser>()
                .eq(TestUser::getUserName, user.getUserName())
                .eq(TestUser::getUserAccount, user.getUserAccount())
                .eq(TestUser::getUserNick, user.getUserNick());
        TestUser one = getOne(wrapper);
        if (one != null) throw new BusinessException(ErrorCode.PARAMS_ERROR, "数据已存在");

        boolean r = this.save(user);
        return r;
    }

    @Override
    public boolean deleteUser(TestUser user) {
        LambdaQueryWrapper<TestUser> wrapper = new LambdaQueryWrapper<TestUser>()
                .eq(TestUser::getUserName, user.getUserName())
                .eq(TestUser::getUserAccount, user.getUserAccount())
                .eq(TestUser::getUserNick, user.getUserNick());
        boolean r = this.remove(wrapper);
        return r;
    }

    @Override
    public boolean updateUser(TestUser user) {
/*        LambdaUpdateWrapper<TestUser> wrapper = new LambdaUpdateWrapper<TestUser>()
                .eq(TestUser::getUserName, user.getUserName())
                .eq(TestUser::getUserAccount, user.getUserAccount())
                .eq(TestUser::getUserNick, user.getUserNick())
                .set(TestUser::getUserNick, user.getUserNick())
                .set(TestUser::getSex, user.getSex());
//                .setEntity(user);
        boolean r = this.update(wrapper);
        return r;*/

        LambdaQueryWrapper<TestUser> wrapper = new LambdaQueryWrapper<TestUser>()
                .eq(TestUser::getUserName, user.getUserName())
                .eq(TestUser::getUserAccount, user.getUserAccount())
                .eq(TestUser::getUserNick, user.getUserNick());
        boolean r = this.update(user, wrapper);
        return r;
    }
}




